/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-08-25 13:07:58
 * @filename : static/nmm/src/views/userList.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module){
    //require("{component}dialog/zepto.dialog.css");
    require('{component}promptTip/promptTip.css');
    var mId = 'userList',reRender = false, 
        userListView, userObj, scrollTop=0, scrollAjax=true, menuName='search',
        panel = require('{component}panel/zepto.panel'),
        imglazyload = require('{component}imglazyload/zepto.imglazyload'),
        promptTip = require('{component}promptTip/promptTip'),
        //dialog = require('{component}dialog/zepto.dialog'),
        tpl = require('{view}userList.tpl'),
        tplN = require('{view}userNav.tpl'),
        tplL = require('{view}userListLi.tpl');

    userListView = Backbone.View.extend({

        el: '',

        template: _.template(tpl),
        templateN: _.template(tplN),
        templateL: _.template(tplL),

        initialize: function () {},

        render: function (obj) {
            if (!share.checkPermission(true)){return;};
            share.reRender(reRender,mId); 
  
            if ($('#'+mId).length < 1) { 
                $('body').append(this.template(lang));
                this.bindEvt();
                var userInfo = share.userInfo();
                userObj = share.getObjStorage(userInfo.usr_id);
                this.setUserListSearch();
            } else {
                $('#'+mId).css('display','block');
            }

            share.panel(panel,this.templateN,mId,menuName); 

            var dom = $('#'+mId).find('.body_all').find('ul');

            if (obj.val == 'Search') {
                dom.find('li').remove();
            } 

            if (dom.find('li').length < 1) {
                this.ajaxList(share.userListSearch); 
            } else {
                (function(y){
                    setTimeout(function() {
                       window.scroll(0,y);
                    },10);
                })(scrollTop);
            }
        },

        setUserListSearch : function() {

            share.userListSearch = {};
            if (userObj.match.match_age_max < 100) {
                share.userListSearch.match_age_max = userObj.match.match_age_max;
            }

            if (userObj.match.match_age_min > 17) {
                share.userListSearch.match_age_min = userObj.match.match_age_min;
            }

            if (userObj.account.country) {
                share.userListSearch.r_country = userObj.account.country;
            }

            if (userObj.account.state) {
                share.userListSearch.match_r_state_id = userObj.account.state;
            }

            if (userObj.match.match_gender !== '') {
                share.userListSearch.match_gender = userObj.match.match_gender;
            }

            share.userListSearch.page_num = 0;
            share.userListSearch.offset = 20;
        },

        bindEvt : function() {
            var tthis = this,dom = $('#'+mId);
            $(window).on('scroll.userList',function(e){
                if(seajs.moduleUI == 'userList') {
                    var WH = $(window).height(),
                    SH = $(window).scrollTop(),
                    //zetpo position:absolte $(document).height() bug /jquery OK
                    DH = $('#'+mId).height();
                    scrollTop = SH;                  
                    if (SH > 0 && (DH-WH-SH) < 100) { 
                        tthis.ajaxList(share.userListSearch);
                        //DH = $('#'+mId).height();
                        //window.scroll(0,DH);
                    }
                    dom.find('.body_all').find('ul').imglazyload({
                        startY : SH,
                        endY:SH + $(window).height()
                    });
 
                }
            });

            dom.find('#backTop').live('tap.userList',function(e){
                if ($(e.target).get(0).id == 'backTop') {
                    window.scroll(0,0);
                    share.backTopEnd(dom.find('.body_all'));
                }
            });

            dom.find('.userList').on('touchstart',function(e) {
                $(this).find('li').css('background','transparent');           
                var li = $(e.target).closest('li');
                li.css('background','#D5DDE0');           
            }).on('touchend',function(e){
                var li = $(e.target).closest('li');
                li.css('background','transparent');           
            });

            //dom.find('.useList').on("li","tap",function(e){
            dom.find('.userList').on('tap',function(e) {
                var uid = $(e.target).closest('li').attr('uid'),
                    userName = $(e.target).closest('li').attr('username');
                if (uid) {
                    if ($(window).height() < 640) {
                        if (panel.info.state == 'hidden') {
                            window.location.href = '#userProfile/whole/custom'+uid+'&&'+userName+'&&userList'; 
                        }
                    } else {
                        window.location.href = '#userProfile/whole/custom'+uid+'&&'+userName+'&&userList'; 
                    }
                }
            });

            dom.find('.left').on('tap',function(e){
                e.stopPropagation();
                share.backTopEnd(dom.find('.body_all'));
                if (panel.info.state == 'hidden') {
                    panel.show();
                } else {
                    panel.hidden(); 
                }
            }); 

            dom.find('.right').on('tap',function(e){
                window.location.href = '#searchOpt/whole'; 
            });

            dom.on('tap',function(){
                if (panel.info.state == 'show' && $(window).width() < 640) {
                    panel.hidden();
                }
            });
        },

        ajaxList : function(data) {
            var tthis = this , dom = $('#'+mId).find('.body_all').find('ul');
            var obj = {
                url : seajs.data.vars.apiAccessUrl+'match_filter',
                type : 'POST',
                data : data,
                success : function(data) {
                    var Img = seajs.data.vars.resourceUrl+'img/';
                    if(data.list.length < 1 && dom.find('li').length < 1) {
                        tthis.noData(dom);
                        return; 
                    } 
                    $('#'+mId).find('.userList').append(tthis.templateL({arr:data.list,defaultImg:Img}));
                    dom.imglazyload({startY : 0,endY: $(window).height()});
                    share.userListSearch['page_num']++;
                    share.userListSearch['hst_id'] = data.hst_id;
                    //share.cacheUserList(data.list);
                },
                error : function(xhr, errorType, error) {
                },
                complete : function(xhr, status) {
                    share.ajaxEndB($('#'+mId).find('ul'));
                    setTimeout(function() {
                        scrollAjax = true;
                    },500)
                }
            };
            if (scrollAjax) {
                promptTip.hide({type:'noData', dom:dom});
                scrollAjax = false;
                share.ajaxStartB(dom);
                $.ajax(share.ajaxControl(obj));
            }
        },

        noData : function(dom) {
            promptTip.show({type:'noData',text:'The best searches are often less restrictive. Give it another shot!', dom:dom});
        },

    });

    module.exports = userListView;
})

